package fragmentson;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.wq.cycling.DemoApplication;
import com.wq.cycling.R;

import activity.SonActivity;
import bean.MessageForSimple;
import fragmentson.backhandle.BackHandledFragment;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import service.ApiUtil;

import static android.app.Activity.RESULT_OK;

/**
 * 添加设备
 * author：wangqi on 2017/4/26 17:48
 * email：773630555@qq.com
 */
public class AddDevice extends BackHandledFragment {

	private View rootView;
	private EditText et_imei;
	//1添加2修改
	private String deviceid="";
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = getLayoutInflater(savedInstanceState).inflate(R.layout.fragment_adddevice, null);
		deviceid  = getArguments().getString("deviceid");
		initView();
		return rootView;
	}

	private void initView() {
		// TODO Auto-generated method stub
		((TextView) rootView.findViewById(R.id.tv_title)).setText(R.string.device_more_imei);
		rootView.findViewById(R.id.tv_next).setOnClickListener(listener);
		rootView.findViewById(R.id.iv_scan).setOnClickListener(listener);
		et_imei = ((EditText) rootView.findViewById(R.id.et_imei));
		rootView.findViewById(R.id.tv_buy).setOnClickListener(listener);
	}

	public static AddDevice newInstance(String deviceid) {
		AddDevice fragment = new AddDevice();
		Bundle bundle = new Bundle();
		bundle.putString("deviceid",deviceid);
		fragment.setArguments(bundle);
		return fragment;
	}

	private View.OnClickListener listener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()){
				case R.id.tv_next:
					ApiUtil.getApiService().setimei(DemoApplication.getToken(),deviceid,et_imei.getText().toString()).enqueue(new Callback<MessageForSimple>() {
						@Override
						public void onResponse(Call<MessageForSimple> call, Response<MessageForSimple> response) {
							try {
								MessageForSimple entity = response.body();
								if (entity.getCode().equals("10008")) {
										((SonActivity) getActivity()).backTask();
								} else {
									Toast.makeText(getActivity(), entity.getMsg(), Toast.LENGTH_LONG).show();
								}
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
						@Override
						public void onFailure(Call<MessageForSimple> call, Throwable t) {
							Toast.makeText(getActivity(), getResources().getString(R.string.error), Toast.LENGTH_LONG).show();
						}
					});
					break;
				case R.id.iv_scan:
					/*Intent intent = new Intent();
					intent.setClass(getActivity(), CaptureActivity.class);
					startActivityForResult(intent, 1);*/
					break;
				case R.id.tv_buy:
					((SonActivity) getActivity()).addFragment(AddGpsDevice.newInstance());
					break;
				default:
					break;
			}
		}
	};

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
			case 1:
				if (resultCode == RESULT_OK) {
					String result = data.getStringExtra("QR_CODE");
					et_imei.setText(result);
				} else {
					//Toast.makeText(getActivity(),"失败", Toast.LENGTH_LONG).show();
				}
				break;
		}
	}

	@Override
	public boolean onBackPressed() {
		return false;
	}
	
}
